Spanish updates from Alberto Nu~nez
[adiumx.git] / Plugins / Purple Service / AMXMLConsoleController.m
blob9a3371fc9e5815561b97c9a05e44b8bbd4f5ba61
1 //
2 //  AMXMLConsoleController.m
3 //  Adium
4 //
5 //  Created by Andreas Monitzer on 2007-06-06.
6 //  Copyright 2007 Andreas Monitzer. All rights reserved.
7 //
9 #import "AMXMLConsoleController.h"
10 #include <libpurple/jabber.h>
11 #import <AIUtilities/AIAutoScrollView.h>
13 #define XML_PREFIX @"<?xml version='1.0' encoding='UTF-8' ?>\n"
15 @interface AMXMLConsoleController (PRIVATE)
16 - (void)appendToLog:(NSAttributedString *)astr;
17 - (PurpleConnection *)gc;
18 @end;
20 static void
21 xmlnode_received_cb(PurpleConnection *gc, xmlnode **packet, gpointer this)
23     AMXMLConsoleController *self = (AMXMLConsoleController *)this;
24     
25     if (!this || [self gc] != gc)
26         return;
27     
28         char *str = xmlnode_to_formatted_str(*packet, NULL);
29     NSString *sstr = [NSString stringWithUTF8String:str];
30     
31     if ([sstr hasPrefix:XML_PREFIX])
32         sstr = [sstr substringFromIndex:[XML_PREFIX length]];
33     
34     NSAttributedString *astr = [[NSAttributedString alloc] initWithString:sstr
35                                                                attributes:nil];
36     [self appendToLog:astr];
37     [astr release];
38     
39         g_free(str);
42 static void
43 xmlnode_sent_cb(PurpleConnection *gc, char **packet, gpointer this)
45     AMXMLConsoleController *self = (AMXMLConsoleController *)this;
46         xmlnode *node;
48     if (!this || [self gc] != gc)
49         return;
51         node = ((*packet && strlen(*packet) && ((*packet)[0] == '<')) ?
52                         xmlnode_from_str(*packet, -1) :
53                         NULL);
55         if (!node)
56                 return;
57         
58         char *str = xmlnode_to_formatted_str(node, NULL);
59     NSString *sstr = [NSString stringWithUTF8String:str];
60     
61     if ([sstr hasPrefix:XML_PREFIX])
62         sstr = [sstr substringFromIndex:[XML_PREFIX length]];
64     NSAttributedString *astr = [[NSAttributedString alloc] initWithString:sstr
65                                                                attributes:[NSDictionary dictionaryWithObject:[NSColor blueColor] forKey:NSForegroundColorAttributeName]];
66     [self appendToLog:astr];
67     [astr release];
68     
69         g_free(str);
70         xmlnode_free(node);
73 @implementation AMXMLConsoleController
75 - (void)dealloc {
76     purple_signals_disconnect_by_handle(self);
77     
78     [super dealloc];
81 - (IBAction)sendXML:(id)sender {
82     NSData *rawXMLData = [[xmlInjectView string] dataUsingEncoding:NSUTF8StringEncoding];
83     jabber_prpl_send_raw(gc, [rawXMLData bytes], [rawXMLData length]);
85     // remove from text field
86     [xmlInjectView setString:@""];
89 - (IBAction)clearLog:(id)sender {
90     [xmlLogView setString:@""];
93 - (IBAction)showWindow:(id)sender {
94         if (!xmlConsoleWindow) {
95                 //Load the window if it's not already loaded
96                 [NSBundle loadNibNamed:@"AMPurpleJabberXMLConsole" owner:self];
97                 if (!xmlConsoleWindow) AILog(@"Unable to load AMPurpleJabberXMLConsole!");
98                 
99                 
100                 //Connect to the signals for updating the window
101                 PurplePlugin *jabber = purple_find_prpl("prpl-jabber");
102                 if (!jabber) AILog(@"Unable to locate jabber prpl");
103                 
104                 purple_signal_connect(jabber, "jabber-receiving-xmlnode", self,
105                                                           PURPLE_CALLBACK(xmlnode_received_cb), self);
106                 purple_signal_connect(jabber, "jabber-sending-text", self,
107                                                           PURPLE_CALLBACK(xmlnode_sent_cb), self);
108         }
109         
110     [xmlConsoleWindow makeKeyAndOrderFront:sender];
111         [(AIAutoScrollView *)[xmlLogView enclosingScrollView] setAutoScrollToBottom:YES];
114 - (void)windowWillClose:(NSNotification *)notification
116         xmlConsoleWindow = nil;
118         //We don't need to watch the signals with the window closed
119         purple_signals_disconnect_by_handle(self);
122 - (void)close
124         [xmlConsoleWindow close];
128 - (void)appendToLog:(NSAttributedString*)astr {
129         [[xmlLogView textStorage] appendAttributedString:astr];
132 - (PurpleConnection*)gc {
133     return gc;
136 - (void)setPurpleConnection:(PurpleConnection *)inGc
138         gc = inGc;
141 @end